﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tools;
using System.IO;
//using ARTemplate;

namespace ScanTemplate
{
    public partial class FormSelectUnScanDir : Form
    {
        public FileDir SelectedDir { get; set; }
        private ScanConfig _sc;
        private int flag = 0;
        public FormSelectUnScanDir()
        {
            InitializeComponent();
            this.AcceptButton = button1;
        }
        private void FormSelectUnScanDir_Load(object sender, EventArgs e)
        {
            if (global.Jsconfig == null || !global.Jsconfig.pathconfig.ExistDirectory())
            {
                this.Close();
                return;
            }
            else
            {
                Init();
            }
        }

        private void Init()
        {
            SelectedDir = null;
            _sc = new ScanConfig(global.Jsconfig.pathconfig.WorkPath, global.Jsconfig.pathconfig.OutPath);
            listBoxUnScanDir.Items.Clear();
            listBoxUnScanDir.Items.AddRange(_sc.Unscans.Unscans.ToArray());
        }
        private void button2_Click(object sender, EventArgs e)
        {
            SelectedDir = null;
            this.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (SelectedDir.Prepapers != null)
            {
                this.Close();
            }
            else
            {
                if (SelectedDir.SubListfiles.Count == 0)
                {
                    if (MessageBox.Show("文件夹为空，是否删除", "删除空文件夹", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        Directory.Delete(SelectedDir.FullPath);
                    }
                    button2_Click(sender, e);
                }
                else
                {    //MessageBox.Show("本文件夹图片已全部识别");
                    if (PreCheckJsonFile(SelectedDir))
                        this.Close();
                    else
                        MessageBox.Show("本文件夹图片未能全部识别");
                }
            }
        }
        private void listBoxUnScanDir_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxUnScanDir.SelectedIndex == -1)
                return;
            SelectedDir = ((UnScan)listBoxUnScanDir.SelectedItem).Filedir();
            SelectedDir.ConstructSubListFile();
            FileDir fd = FileDir.ExistImageABPage(SelectedDir.SubListfiles);
            if (fd != null)
                SelectedDir = fd;

           string msg="共有文件："+   SelectedDir.SubListfiles.Count + "个";
           if (SelectedDir.ABPage)
               msg += "  存在正反面";
           else
               msg += "  不存在正反面";
           textBoxMsg.Text = msg;           
        }
        private void listBoxUnScanDir_KeyUp(object sender, KeyEventArgs e)
        {
            if (listBoxUnScanDir.SelectedIndex == -1) return;
            UnScan dir = (UnScan)listBoxUnScanDir.SelectedItem;

            if (e.KeyCode == Keys.O)
            {
                if (Directory.Exists(dir.Fullpath))
                    System.Diagnostics.Process.Start(dir.Fullpath);
            }
            else if (e.KeyCode == Keys.Delete)
            { //删除未扫描数据
                //FormInput f = new FormInput("删除确认");
                //f.ShowDialog();
                //if (f.StrValue == null || f.StrValue != "Del")
                //    return;
                //if (dir.DirName == "")
                //    return;
                //if (Directory.Exists(dir.Fullpath))
                //    Directory.Delete(dir.Fullpath, true);
                ////某些清理工作
                //Init();
            }
            else if (e.KeyCode == Keys.P)
            {
                FileDir fd = dir.Filedir();
                fd.InitEmptyPrepapers();
                FormPreScan fps = new FormPreScan(fd);
                ShowFormPreScan(fps);
            }
            else if (e.KeyData == Keys.Enter)
            {
                flag++;
                if (flag > 1)
                    button1.PerformClick();
            }
            else if (e.KeyData == Keys.Escape)
            {
                button2.PerformClick();
            }
        }
        private void listBoxUnScanDir_DoubleClick(object sender, System.EventArgs e)
        {
            if (listBoxUnScanDir.SelectedIndex != -1)
                button1.PerformClick();
        }
        private  bool PreCheckJsonFile(FileDir fd)
        {
            global.ABPage = fd.ABPage;

            PreScanCheck  _ActivePreScanCheck = new PreScanCheck(fd);
            if (!_ActivePreScanCheck.PreCheckJsonFile())
            {
                FormPreScan fps = new FormPreScan(fd);
                ShowFormPreScan(fps);
                _ActivePreScanCheck = null;
                return false;
            }
            fd.Prepapers = _ActivePreScanCheck.Prepapers;
            return true;
        }
        private void ShowFormPreScan(FormPreScan fps)
        {
            this.Hide();
            if (global.UserMode)
                fps.SetUserMode();
            fps.ShowDialog();
            fps.Clear();
            fps = null;
            this.Show();
        }
    }
}
